//JDBCDAO.java
//Classe mãe das daos do JDBC
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author Diego Heusser
 * @author Eder Schramm
 */
public class JDBCDAO {

    protected Connection conexao;

    public JDBCDAO() {
        Conexao con = new Conexao();
        this.conexao = con.getConnectionJDBC();
    }

    public Connection getConexao() {
        return conexao;
    }

    protected void insere(String insereSQL, Object... parametros) throws SQLException {
        PreparedStatement pstmt = getConexao().prepareStatement(insereSQL);

        for (int i = 0; i < parametros.length; i++) {
            pstmt.setObject(i + 1, parametros[i]);
        }
        pstmt.execute();
        pstmt.close();

    }

    protected void altera(String alteraSQL, Object id, Object... parametros) throws SQLException {
        PreparedStatement pstmt = getConexao().prepareStatement(alteraSQL);
        for (int i = 0; i < parametros.length; i++) {
            pstmt.setObject(i + 1, parametros[i]);
        }
        pstmt.setObject(parametros.length + 1, id);
        pstmt.execute();
        pstmt.close();

    }

    protected void remove(String removeSQL, Object... parametros) throws SQLException {
        PreparedStatement pstmt = getConexao().prepareStatement(removeSQL);
        for (int i = 0; i < parametros.length; i++) {
            pstmt.setObject(i + 1, parametros[i]);
        }
        pstmt.execute();
        pstmt.close();
    }
}
